package com.ck.framework.security.handle;

import com.ck.common.bean.ResponseResult;
import com.ck.common.utils.json.JacksonUtil;
import com.ck.common.utils.web.WebUtils;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 认证失败处理类 返回未授权(认证是验证用户的身份)
 * @author ck
 */
@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException)  {
        ResponseResult result = new ResponseResult(HttpStatus.UNAUTHORIZED.value(), "认证失败");
        String json = JacksonUtil.obj2json(result);
        WebUtils.renderString(response,json);
    }
}


